package org.come.Jpanel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import org.come.bean.LoginResult;
import org.come.entity.Pal;
import org.come.model.PalData;
import org.come.until.*;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.PartnerBtn;
import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

/**
 * @author HGC<br>
 * @time 2019年12月20日 上午9:44:41<br>
 * @class 类名:PartnerMainJpanel 伙伴主面板 编号：105<br>
 */
public class PartnerMainJpanel extends JPanel {

    /**
     * 字段名:scrollPane 滚动条伙伴<br>
     */
    private JScrollPane scrollPane;

    /**
     * 字段名:partnerCardJpanel 卡片布局<br>
     */
    private PartnerCardJpanel partnerCardJpanel;

    /**
     * 字段名:btnTeam 队伍菜单按钮caozuo=1<br>
     * 字段名:btnSkill 技能菜单按钮caozuo=2<br>
     * 字段名:btnEquip 装备菜单按钮caozuo=3<br>
     */
    private PartnerBtn btnTeam, btnSkill, btnEquip;

    /**
     * 字段名:partnerUnitJpanels 伙伴集合<br>
     */
    private JList<PartnerUnitJpanel> partnerUnitJpanels;
    private DefaultListModel<PartnerUnitJpanel> defaultListModel;

    /**
     * 字段名:palData 选中的伙伴<br>
     */
    private int palDataChooseId;
    private int palLvl;//伙伴等级

    public PartnerMainJpanel() {
        String style = MyIsif.getStyle();
        if ("水墨UI".equals(style)) {
            //
            this.setOpaque(false);
            setPreferredSize(new Dimension(563, 383));
            setLayout(null);
            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/button/1.png", 1, 105);
            offBtn.setBounds(563 - 37, 10, 25, 25);
            this.add(offBtn);
            partnerCardJpanel = new PartnerCardJpanel();
            partnerCardJpanel.setBounds(205, 70, 356, 301);
            this.add(partnerCardJpanel);
        } else {
            //
            this.setOpaque(false);
            setPreferredSize(new Dimension(563, 383));
            setLayout(null);
            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/hongmu/s74.png", 1, 105);
            offBtn.setBounds(524, 5, 25, 25);
            this.add(offBtn);
            partnerCardJpanel = new PartnerCardJpanel();
            partnerCardJpanel.setBounds(205, 70, 356, 301);
            this.add(partnerCardJpanel);
        }

        getBtnTeam();
        getBtnSkill();
        getBtnEquip();
        getScrollPane();

    }

    /**
     * 字段名:type 初始化标识<br>
     */
    int type = -1;

    /**
     * 添加伙伴信息
     */
    public void addPartnerUnit() {
        if (this.type == -1) {
            List<Pal> pals = RoleData.getRoleData().getPals();
            if (pals == null) {
                return;
            }
            this.type = 1;
            String loPals = RoleData.getRoleData().getLoginResult().getPals();
            String[] palsArr = null;
            if (loPals != null) {
                palsArr = loPals.split("\\|");
            }
            Set<Entry<Integer, PalData>> entrySet = UserMessUntil.getAllPal().getAllPalData().entrySet();

            Integer grade = RoleData.getRoleData().getLoginResult().getGrade();
            String lvl = null;
            if (palLvl != grade) {
                palLvl = grade;
                lvl = AnalysisString.lvl(palLvl);
            }
            S:
            for (Entry<Integer, PalData> entry : entrySet) {
                PalData palData = entry.getValue();
                PartnerUnitJpanel partnerUnitJpanel = new PartnerUnitJpanel();
                partnerUnitJpanel.showPal(palData, pidGetPal(palData.getPalId()), palsArr, lvl);
                for (int i = 0, length = defaultListModel.size(); i < length; i++) {
                    PartnerUnitJpanel unitJpanel = defaultListModel.get(i);
                    if (partnerUnitJpanel.getState() == -1) {
                        if (unitJpanel.getState() == -1 && partnerUnitJpanel.getPid() < unitJpanel.getPid()) {
                            defaultListModel.add(i, partnerUnitJpanel);
                            continue S;
                        }
                    } else if (partnerUnitJpanel.getState() == 0) {
                        if (unitJpanel.getState() == -1) {
                            defaultListModel.add(i, partnerUnitJpanel);
                            continue S;
                        } else if (unitJpanel.getState() == 0 && partnerUnitJpanel.getPid() < unitJpanel.getPid()) {
                            defaultListModel.add(i, partnerUnitJpanel);
                            continue S;
                        }
                    } else {
                        if (unitJpanel.getState() == -1 || unitJpanel.getState() == 0) {
                            defaultListModel.add(i, partnerUnitJpanel);
                            continue S;
                        } else if (partnerUnitJpanel.getState() < unitJpanel.getState()) {
                            defaultListModel.add(i, partnerUnitJpanel);
                            continue S;
                        }
                    }
                }
                defaultListModel.addElement(partnerUnitJpanel);
            }
            partnerUnitJpanels.setPreferredSize(new Dimension(137, 50 * entrySet.size()));
        } else {
            refreshLvl();
        }
        FormsManagement.showForm(105);
    }

    /**
     * 刷新伙伴等级
     */
    public void refreshLvl() {
        Integer grade = RoleData.getRoleData().getLoginResult().getGrade();
        if (grade == palLvl) {
            return;
        }
        palLvl = grade;
        String lvl = AnalysisString.lvl(palLvl);
        String lvlStr = lvl + "级";
        for (int i = 0, length = defaultListModel.size(); i < length; i++) {
            PartnerUnitJpanel unitJpanel = defaultListModel.get(i);
            unitJpanel.getLabLvl().setText(lvlStr);
        }
        if (palDataChooseId != 0) {
            partnerCardJpanel.getPartnerTeamJpanel().getLabMessage()[2].setText(lvlStr);
        }
    }

    /**
     * Pid获取界面
     */
    public PartnerUnitJpanel getPidJpanel(int pid) {
        for (int i = 0, length = defaultListModel.size(); i < length; i++) {
            PartnerUnitJpanel unitJpanel = defaultListModel.get(i);
            if (pid == unitJpanel.getPid()) {
                return unitJpanel;
            }
        }
        return null;
    }

    /**
     * 刷新
     *
     * @param palList 不为空刷新 为空初始化
     */
    public void refreshPals(List<Pal> palList) {
        String loPals = RoleData.getRoleData().getLoginResult().getPals();
        String[] palsArr = null;
        if (loPals != null) {
            palsArr = loPals.split("\\|");
        }
        Integer grade = RoleData.getRoleData().getLoginResult().getGrade();
        String lvl = null;
        if (grade != palLvl) {
            palLvl = grade;
            lvl = AnalysisString.lvl(grade);
        }
        for (int i = 0, length = palList.size(); i < length; i++) {
            Pal pal = palList.get(i);
            PalData palData = UserMessUntil.getPalData(pal.getpId());
            PartnerUnitJpanel partnerUnitJpanel = getPidJpanel(pal.getpId());
            int oState = partnerUnitJpanel.getState();
            int nState = partnerUnitJpanel.changePalState(pidGetPal(palData.getPalId()), palsArr, lvl);
            partnerUnitJpanel.setState(nState);
            if (oState != nState) {
                defaultListModel.removeElement(partnerUnitJpanel);
                boolean is = true;
                for (int i1 = 0, length2 = defaultListModel.size(); i1 < length2; i1++) {
                    PartnerUnitJpanel unitJpanel = defaultListModel.get(i1);
                    if (partnerUnitJpanel.getState() == -1) {
                        if (unitJpanel.getState() == -1 && partnerUnitJpanel.getPid() < unitJpanel.getPid()) {
                            defaultListModel.add(i1, partnerUnitJpanel);
                            is = false;
                            break;
                        }
                    } else if (partnerUnitJpanel.getState() == 0) {
                        if (unitJpanel.getState() == -1) {
                            defaultListModel.add(i1, partnerUnitJpanel);
                            is = false;
                            break;
                        } else if (unitJpanel.getState() == 0 && partnerUnitJpanel.getPid() < unitJpanel.getPid()) {
                            defaultListModel.add(i1, partnerUnitJpanel);
                            is = false;
                            break;
                        }
                    } else {
                        if (unitJpanel.getState() == -1 || unitJpanel.getState() == 0) {
                            defaultListModel.add(i1, partnerUnitJpanel);
                            is = false;
                            break;
                        } else if (partnerUnitJpanel.getState() < unitJpanel.getState()) {
                            defaultListModel.add(i1, partnerUnitJpanel);
                            is = false;
                            break;
                        }
                    }
                }
                if (is) {
                    defaultListModel.addElement(partnerUnitJpanel);
                }
            }
            if (palDataChooseId == pal.getpId()) {
//                partnerCardJpanel.getPartnerEquipJpanel().showExpNum(pal);
                partnerCardJpanel.getPartnerEquipJpanel().ShowEquipArr(pal);
                partnerCardJpanel.getPartnerTeamJpanel().showPalMessage(pal, palData);
                partnerUnitJpanel.changeWarBtn();
            }
        }
    }

    /**
     * 根据Pid获取对应的Pal
     *
     * @param chooseId
     * @return
     */
    public Pal pidGetPal(int chooseId) {
        List<Pal> pals = RoleData.getRoleData().getPals();
        for (int i = 0; i < pals.size(); i++) {
            Pal pal = pals.get(i);
            if (pal.getpId() == chooseId) {
                return pal;
            }
        }
        return null;
    }

    /**
     * 根据id获取对应的Pal
     *
     * @param id
     * @return
     */
    public Pal idGetPal(BigDecimal id) {
        List<Pal> pals = RoleData.getRoleData().getPals();
        for (int i = 0; i < pals.size(); i++) {
            Pal pal = pals.get(i);
            if (pal.getId().compareTo(id) == 0) {
                return pal;
            }
        }
        return null;
    }

    private ImageIcon iconBack;

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        String style = MyIsif.getStyle();
        if ("水墨UI".equals(style)) {
            //
            if (iconBack == null) {
                iconBack = new ImageIcon("inkImg/newbackground/B303.png");
            }
            g.drawImage(iconBack.getImage(), 0, 0, 563, 383, this);

        } else {
            //
            if (iconBack == null) {
                iconBack = new ImageIcon("img/xy2uiimg/S149.png");
            }
            g.drawImage(iconBack.getImage(), 0, 0, 563, 383, this);
        }

    }

    public JScrollPane getScrollPane() {
        if (scrollPane == null) {
            // 属性滚动条
            scrollPane = new JScrollPane();
            scrollPane.setVerticalScrollBarPolicy(22);
            scrollPane.getViewport().setView(getPartnerUnitJpanels());
            scrollPane.getVerticalScrollBar().setUI(new ScrollUI());
            scrollPane.getVerticalScrollBar().setUnitIncrement(20);
            scrollPane.getViewport().setOpaque(false);
            scrollPane.setOpaque(false);
            String style = MyIsif.getStyle();
            if ("水墨UI".equals(style)) {
                //
                scrollPane.setBounds(46, 52, 152, 312);
            } else {
                //
                scrollPane.setBounds(47, 66, 148, 280);
            }
            scrollPane.setBorder(BorderFactory.createEmptyBorder());
            scrollPane.setHorizontalScrollBarPolicy(31);
            this.add(scrollPane);
        }
        return scrollPane;
    }

    public void setScrollPane(JScrollPane scrollPane) {
        this.scrollPane = scrollPane;
    }

    public PartnerCardJpanel getPartnerCardJpanel() {

        return partnerCardJpanel;
    }

    public void setPartnerCardJpanel(PartnerCardJpanel partnerCardJpanel) {
        this.partnerCardJpanel = partnerCardJpanel;
    }

    public ImageIcon getIconBack() {
        return iconBack;
    }

    public void setIconBack(ImageIcon iconBack) {
        this.iconBack = iconBack;
    }

    public PartnerBtn getBtnTeam() {
        String style = MyIsif.getStyle();
        if ("水墨UI".equals(style)) {
            //
            if (btnTeam == null) {
                btnTeam = new PartnerBtn("inkImg/newbutton/K78.png", 1, 1, this);
                btnTeam.setBounds(215, 26, 75, 33);
                this.add(btnTeam);
            }
        } else {
            //
            if (btnTeam == null) {
                btnTeam = new PartnerBtn("img/xy2uiimg/B229.png", 1, 1, this);
                btnTeam.setBounds(215, 25, 75, 35);
                this.add(btnTeam);
            }
        }
        return btnTeam;
    }

    public void setBtnTeam(PartnerBtn btnTeam) {
        this.btnTeam = btnTeam;
    }

    public PartnerBtn getBtnSkill() {
        String style = MyIsif.getStyle();
        if ("水墨UI".equals(style)) {
            //
            if (btnSkill == null) {
                btnSkill = new PartnerBtn("inkImg/newbutton/K79.png", 1, 2, this);
                btnSkill.setBounds(215 + 78, 26, 75, 33);
                this.add(btnSkill);
            }
        } else {
            //
            if (btnSkill == null) {
                btnSkill = new PartnerBtn("img/xy2uiimg/B232.png", 1, 2, this);
                btnSkill.setBounds(215 + 72, 25, 75, 35);
                this.add(btnSkill);
            }
        }
        return btnSkill;
    }

    public void setBtnSkill(PartnerBtn btnSkill) {
        this.btnSkill = btnSkill;
    }

    public PartnerBtn getBtnEquip() {
        String style = MyIsif.getStyle();
        if ("水墨UI".equals(style)) {
            //
            if (btnEquip == null) {
                btnEquip = new PartnerBtn("inkImg/newbutton/K81.png", 1, 3, this);
                btnEquip.setBounds(215 + 78 * 2, 26, 75, 33);
                this.add(btnEquip);
            }
        } else {
            //
            if (btnEquip == null) {
                btnEquip = new PartnerBtn("img/xy2uiimg/B234.png", 1, 3, this);
                btnEquip.setBounds(215 + 72 * 2, 25, 75, 35);
                this.add(btnEquip);
            }
        }
        return btnEquip;
    }

    public void setBtnEquip(PartnerBtn btnEquip) {
        this.btnEquip = btnEquip;
    }

    public JList<PartnerUnitJpanel> getPartnerUnitJpanels() {
        String style = MyIsif.getStyle();
        if ("水墨UI".equals(style)) {
            //
            if (partnerUnitJpanels == null) {
                partnerUnitJpanels = new JList<PartnerUnitJpanel>();
                partnerUnitJpanels.setSelectionBackground(new Color(122, 117, 112));
                partnerUnitJpanels.setSelectionForeground(Color.white);
                partnerUnitJpanels.setForeground(Color.white);
                partnerUnitJpanels.setFont(UIUtils.TEXT_HY16);
                partnerUnitJpanels.setBackground(UIUtils.Color_BACK); // 设置列表框为透明背景
                DefaultListCellRenderer cellRenderer = new DefaultListCellRenderer() {
                    @Override
                    public Component getListCellRendererComponent(javax.swing.JList<?> list, Object value, int index,
                                                                  boolean isSelected, boolean cellHasFocus) {
                        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                        if (value instanceof PartnerUnitJpanel) {
                            PartnerUnitJpanel unitJpanel = (PartnerUnitJpanel) value;
                            return unitJpanel;
                        }
                        return this;
                    }
                };
                partnerUnitJpanels.setCellRenderer(cellRenderer);
                partnerUnitJpanels.addMouseListener(new MouseListener() {

                    @Override
                    public void mouseReleased(MouseEvent e) {

                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        PartnerUnitJpanel unitJpanel = partnerUnitJpanels.getSelectedValue();
                        if (unitJpanel == null) {
                            return;
                        }
                        palDataChooseId = unitJpanel.getPid();
                        LoginResult loginResult = RoleData.getRoleData().getLoginResult();
                        Integer grade = loginResult.getGrade();
                        String lvl = null;
                        if (grade != palLvl) {
                            palLvl = grade;
                            lvl = AnalysisString.lvl(palLvl);
                        }
                        String pals = loginResult.getPals();
                        // List<Pal> palsList = RoleData.getRoleData().getPals();
                        String[] palsArr = null;
                        if (pals != null && !"".equals(pals)) {
                            palsArr = pals.split("\\|");
                        }
                        Pal pidGetPal = pidGetPal(palDataChooseId);
                        unitJpanel.setState(unitJpanel.changePalState(pidGetPal, palsArr, lvl));
                        unitJpanel.changeWarBtn();

                        partnerCardJpanel.getPartnerTeamJpanel().hideBtnArr();
                        partnerCardJpanel.getPartnerTeamJpanel().showPalMessage(pidGetPal, UserMessUntil.getPalData(palDataChooseId));
                        /** 技能面板 */
                        partnerCardJpanel.getPartnerSkillJpanel().showBornSkill(palDataChooseId);
                        /** 装备面板 */
                        // 展示经验
//                        partnerCardJpanel.getPartnerEquipJpanel().showExpNum(pidGetPal);
                        // 展示装备
                        partnerCardJpanel.getPartnerEquipJpanel().ShowEquipArr(pidGetPal);
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {

                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {

                    }

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        // TODO Auto-generated method stub

                    }
                });
                partnerUnitJpanels.setOpaque(false);
                partnerUnitJpanels.setModel(getDefaultListModel());
            }
        } else {
            //
            if (partnerUnitJpanels == null) {
                partnerUnitJpanels = new JList<PartnerUnitJpanel>();
                partnerUnitJpanels.setSelectionBackground(new Color(122, 117, 112));
                partnerUnitJpanels.setSelectionForeground(Color.white);
                partnerUnitJpanels.setForeground(Color.white);
                partnerUnitJpanels.setFont(UIUtils.TEXT_HY16);
                partnerUnitJpanels.setBackground(UIUtils.Color_BACK); // 设置列表框为透明背景
                DefaultListCellRenderer cellRenderer = new DefaultListCellRenderer() {
                    @Override
                    public Component getListCellRendererComponent(javax.swing.JList<?> list, Object value, int index,
                                                                  boolean isSelected, boolean cellHasFocus) {
                        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                        if (value instanceof PartnerUnitJpanel) {
                            PartnerUnitJpanel unitJpanel = (PartnerUnitJpanel) value;
                            return unitJpanel;
                        }
                        return this;
                    }
                };
                partnerUnitJpanels.setCellRenderer(cellRenderer);
                partnerUnitJpanels.addMouseListener(new MouseListener() {

                    @Override
                    public void mouseReleased(MouseEvent e) {

                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        PartnerUnitJpanel unitJpanel = partnerUnitJpanels.getSelectedValue();
                        if (unitJpanel == null) {
                            return;
                        }
                        palDataChooseId = unitJpanel.getPid();
                        LoginResult loginResult = RoleData.getRoleData().getLoginResult();
                        Integer grade = loginResult.getGrade();
                        String lvl = null;
                        if (grade != palLvl) {
                            palLvl = grade;
                            lvl = AnalysisString.lvl(palLvl);
                        }
                        String pals = loginResult.getPals();
                        // List<Pal> palsList = RoleData.getRoleData().getPals();
                        String[] palsArr = null;
                        if (pals != null && !"".equals(pals)) {
                            palsArr = pals.split("\\|");
                        }
                        Pal pidGetPal = pidGetPal(palDataChooseId);
                        unitJpanel.setState(unitJpanel.changePalState(pidGetPal, palsArr, lvl));
                        unitJpanel.changeWarBtn();

                        partnerCardJpanel.getPartnerTeamJpanel().hideBtnArr();
                        partnerCardJpanel.getPartnerTeamJpanel().showPalMessage(pidGetPal, UserMessUntil.getPalData(palDataChooseId));
                        /** 技能面板 */
                        partnerCardJpanel.getPartnerSkillJpanel().showBornSkill(palDataChooseId);
                        /** 装备面板 */
                        // 展示经验
//                    partnerCardJpanel.getPartnerEquipJpanel().showExpNum(pidGetPal);
                        // 展示装备
                        partnerCardJpanel.getPartnerEquipJpanel().ShowEquipArr(pidGetPal);
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {

                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {

                    }

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        // TODO Auto-generated method stub

                    }
                });
                partnerUnitJpanels.setOpaque(false);
                partnerUnitJpanels.setModel(getDefaultListModel());
            }
        }
        return partnerUnitJpanels;
    }

    public void setPartnerUnitJpanels(JList<PartnerUnitJpanel> partnerUnitJpanels) {
        this.partnerUnitJpanels = partnerUnitJpanels;
    }

    public int getPalDataChooseId() {
        return palDataChooseId;
    }

    public void setPalDataChooseId(int palDataChooseId) {
        this.palDataChooseId = palDataChooseId;
    }

    public DefaultListModel<PartnerUnitJpanel> getDefaultListModel() {
        if (defaultListModel == null) {
            defaultListModel = new DefaultListModel<>();
        }
        return defaultListModel;
    }

    public void setDefaultListModel(DefaultListModel<PartnerUnitJpanel> defaultListModel) {
        this.defaultListModel = defaultListModel;
    }

}
